package mcom.xmks.bluetooth.mesh.dialog;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

//import kotlin.text.Regex;
import mcom.xmks.bluetooth.mesh.R;
import mcom.xmks.bluetooth.mesh.storage.GroupStorageUtil;
import mcom.xmks.bluetooth.mesh.utils.ToastUtil;

public class InputGroupInfoDialog extends BaseDialogFragment{
    private View cancel,confirm;
    private TextInputEditText edit_group_addr,edit_group_name;
    private TextInputLayout tiLayout;
    private OnClickListener listener;
    @Override
    protected View onCreateRealView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.dialog_create_group,null);
        tiLayout=view.findViewById(R.id.tiLayout);
        edit_group_name=view.findViewById(R.id.edit_group_name);
        edit_group_addr=view.findViewById(R.id.edit_group_addr);
        cancel=view.findViewById(R.id.cancel);
        confirm=view.findViewById(R.id.confirm);
        initWidget();
        return view;
    }
    protected final void initWidget(){
        cancel.setOnClickListener(v -> {
            dismiss();
        });
        confirm.setOnClickListener(v->{
            check();
        });
        edit_group_addr.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
               String address=s.toString();
               if(address.isEmpty()){
                   tiLayout.setError("cannot be empty!");
               }else if(address.matches("([0-9|a-f|A-F]{2}){2}")){
                   tiLayout.setError("invalid address");
               }else{
                   tiLayout.setError(null);
               }
            }
        });
    }
    private void check(){
        String groupName=edit_group_name.getText().toString();
        String groupAddr=edit_group_addr.getText().toString();
        if(groupAddr!=null) {
            if (!groupAddr.matches("([0-9|a-f|A-F]{2}){2}")) {
                showToast("地址必须为合法的2字节");
                return;
            }
            //check name
            if (groupName == null || groupName.isEmpty()) {
                showToast("Group名称为空");
                return;
            }
            int addressData= Integer.parseInt(groupAddr,16);
            if(addressData<0x0000C000){
                showToast("Group地址应大于C000");
                return;
            }
            if(GroupStorageUtil.checkGroupHasExist(addressData)){
                showToast("Group地址已经存在");
                return;
            }
            if(listener!=null){
                listener.onCreateGroup(addressData,groupName);
            }
            dismiss();
        }
    }
    public void setCreateGroupListener(OnClickListener listener){
        this.listener=listener;
    }
    public interface OnClickListener{
        void onCreateGroup(int address, String name);
    }
    public void showToast(String message){
        ToastUtil.create(requireActivity(),message);
    }
}
